<?php



/* ============================ PREMIUM PRESS CUSTOM FIELD FUNCTION ======================== */

if(!function_exists('get_custom_field')) {
	function get_custom_field($field) {
		global $post;
		$custom_field = get_post_meta($post->ID, $field, true);
		echo $custom_field;
	}
}

/* ============================ PREMIUM PRESS CUSTOM FIELD FUNCTION ======================== */

function premiumpress_customfields_box() {
	if( function_exists( 'add_meta_box' )) {
		add_meta_box( 'premiumpress_customfields_1', __( 'Product Details', 'sp' ), 'premiumpress_product', 'post', 'normal', 'high' );
	add_meta_box( 'premiumpress_customfields_2', __( 'Image Details', 'sp' ), 'premiumpress_images', 'post', 'normal', 'high' );
	//add_meta_box( 'premiumpress_customfields_2', __( 'Custom Lists', 'sp' ), 'premiumpress_custom', 'post', 'normal', 'high' );

	}
}

/* ============================ PREMIUM PRESS CUSTOM FIELD FUNCTION ======================== */
 

function premiumpress_product(){

	global $post;
	
	// Use nonce for verification ... ONLY USE ONCE!
	echo '<input type="hidden" name="sp_noncename" id="sp_noncename" value="' . 
	wp_create_nonce( plugin_basename(__FILE__) ) . '" />';


	echo '<table width="100%"  border="0"><tr width="50%"><td valign="top">';

		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Product SKU</b> (optional)", 'sp' ) . '</label><br />';
		echo '<input type="text" name="SKU" value="'.get_post_meta($post->ID, 'SKU', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;"/><br /><br />';


		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Current Price</b> (numeric value, dont add the currency symbol)", 'sp' ) . '</label><br />';
		echo '<input type="text" name="price" value="'.get_post_meta($post->ID, 'price', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;"/><br /><br />';

		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Old Price</b> (optional)", 'sp' ) . '</label><br />';
		echo '<input type="text" name="old_price" value="'.get_post_meta($post->ID, 'old_price', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;"/><br /><br />';


	echo '</td><td width="50%" valign="top">';


		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Quantity</b> ", 'sp' ) . '</label><br />';
		echo '<input type="text" name="qty" value="'.get_post_meta($post->ID, 'qty', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;" /><br /><br />';

		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Weight</b> (numeric value, dont add the metric) ", 'sp' ) . '</label><br />';
		echo '<input type="text" name="weight" value="'.get_post_meta($post->ID, 'weight', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;" /><br /><br />';
 
		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Warranty</b> ", 'sp' ) . '</label><br />';
		echo '<input type="text" name="warranty" value="'.get_post_meta($post->ID, 'warranty', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;" /><br /><br />';

		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Filename</b> (used for download stores only)", 'sp' ) . '</label><br />';
		echo '<input type="text" name="file" value="'.get_post_meta($post->ID, 'file', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;"/><br /><br />';
 
	echo '</td></tr></table>';

}


function premiumpress_images() {

	global $post;

	// ADD FOR CUSTOM CHECKS
	echo '<input type="hidden" name="sp_noncename" id="sp_noncename" value="' . 
	wp_create_nonce( plugin_basename(__FILE__) ) . '" />';


	echo '<table width="100%"  border="0"><tr width="50%"><td valign="top">';

		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Main Product Image</b> (example: imagename.jpg or http://../imagename.jpg)", 'sp' ) . '</label><br />';
		echo '<input type="text" name="image" value="'.get_post_meta($post->ID, 'image', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;"/><br /><br />';

		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Search Page Thumbnail</b> (optional extra image)", 'sp' ) . '</label><br />';
		echo '<input type="text" name="thumbnail" value="'.get_post_meta($post->ID, 'thumbnail', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;"/><br /><br />';

		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Featured Image</b> (optional, used for homepage slider only)", 'sp' ) . '</label><br />';
		echo '<input type="text" name="featured_image" value="'.get_post_meta($post->ID, 'featured_image', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3;"/><br /><br />';

	echo '</td><td width="50%" valign="top">';

		echo '<label style="font-size:14px; line-height:30px;">' . __("<b>Product Gallery Images</b> (seperate multiple images with a comma)", 'sp' ) . '</label><br />';
		echo '<input type="text" name="images" value="'.get_post_meta($post->ID, 'images', true).'" style="font-size:14px;padding:5px; width:95%; background:#e7fff3; height:180px;" /><br /><br />';
 

	echo '</td></tr></table>';
	
}

/* ============================ PREMIUM PRESS CUSTOM FIELD FUNCTION ======================== */

function premiumpress_postdata($post_id, $post) {
	
	if ( !wp_verify_nonce( $_POST['sp_noncename'], plugin_basename(__FILE__) )) {
		return $post->ID;
	}

	// Is the user allowed to edit the post or page?
	if ( 'page' == $_POST['post_type'] ) {
		if ( !current_user_can( 'edit_page', $post->ID ))
		return $post->ID;
	} else {
		if ( !current_user_can( 'edit_post', $post->ID ))
		return $post->ID;
	}

	$mydata = array();

	$mydata['thumbnail'] 		= $_POST['thumbnail'];
	$mydata['image'] 			= $_POST['image'];
	$mydata['images'] 			= $_POST['images'];
	$mydata['featured_image'] 	= $_POST['featured_image'];

	$mydata['price'] 			= $_POST['price'];
	$mydata['old_price'] 		= $_POST['old_price'];
	$mydata['file'] 			= $_POST['file'];
	$mydata['qty'] 				= $_POST['qty'];
	$mydata['weight'] 			= $_POST['weight'];
	$mydata['warranty'] 		= $_POST['warranty'];
	$mydata['SKU'] 				= $_POST['SKU'];


	
	foreach ($mydata as $key => $value) {
		if( $post->post_type == 'revision' ) return;
		$value = implode(',', (array)$value); 
		if(get_post_meta($post->ID, $key, FALSE)) { 
			update_post_meta($post->ID, $key, $value);
		} else { 
			add_post_meta($post->ID, $key, $value);
		}
		if(!$value) delete_post_meta($post->ID, $key);
	}

}

/* ============================ PREMIUM PRESS CUSTOM FIELD FUNCTION ======================== */

add_action('admin_menu', 'premiumpress_customfields_box');
add_action('save_post',  'premiumpress_postdata', 1, 2);


/* ============================ PREMIUM PRESS CUSTOM FIELD FUNCTION ======================== */

add_filter( 'manage_posts_columns', 'premiumpress_columns' );
add_action('manage_posts_custom_column', 'premiumpress_custom_column', 10, 2); //Just need a single function to add multiple columns

add_filter('manage_posts_columns', 'scompt_custom_columns');

function scompt_custom_columns($defaults) {

   // unset($defaults['comments']);
    unset($defaults['author']);
    unset($defaults['tags']);
 
    return $defaults;
}
function premiumpress_columns($defaults) {

    $defaults['price'] 		= __('Price');
   // $defaults['qty'] 		= __('Quantity');
    $defaults['image'] 		= __('Display Image');

    return $defaults;
}


function premiumpress_custom_column($column_name, $post_id) {

    global $wpdb;

	$CurrencySymbol = get_option("currency_symbol");

    if( $column_name == 'price' ) {

			$price = get_post_meta($post_id, "price", true);
			if ( !empty( $price ) ) {

				print $CurrencySymbol."".$price;
			} else {
				_e('No Price Set');  //No Taxonomy term defined
			}

	} else if( $column_name == 'qty' ) {   //Adding 2nd column

			$qty = get_post_meta($post_id, "qty", true);
			if ( !empty( $qty ) ) {

				print $qty." in Stock";
			} else {
				_e('');
			}

	} else if( $column_name == 'image' ) {   //Adding 2nd column

			$img = get_post_meta($post_id, "image", true);
			if ( !empty( $img ) && strlen($img) > 2 ) {
				echo "<img src='".SPImage($img,"s")."' style='max-height:80px'>";
			} else {
				_e('<font color="red">No Image Set</font>');
			}

	} else {
            echo '<i>'.$column_name.'</i>'; //Only 2 columns, blank now.
        }

}


 

?>